//
//  ExamResultViewController.m
//  jinnengExam
//
//  Created by mac on 2020/8/12.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "ExamResultViewController.h"
#import "PBFlatBarButtonItems.h"
#import "AnswerSheetViewController.h"

@interface ExamResultViewController ()

@end

@implementation ExamResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"成绩";
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [ColorUtil navigationBarColor:[ColorUtil titleColor] BarSize:bar.frame alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    [bar setTintColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    UIBarButtonItem *back = [PBFlatBarButtonItems backBarButtonItemWithTarget:self selector:@selector(goBack)];
    [self.navigationItem setLeftBarButtonItem:back];
    
    self.tabBarController.tabBar.frame = CGRectMake(0, 0, 0, 0);
     self.tabBarController.tabBar.hidden = YES;
    // Do any additional setup after loading the view.
    [self initView];
}

-(void) viewWillAppear:(BOOL)animated{
       self.navigationController.navigationBar.hidden = NO;
}

-(void) initView{
   [self.view addSubview:self.jznWebView];
    [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.right.equalTo(self.view);
         if (@available(iOS 11.0, *)) {
             make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
             make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
         } else {
             make.top.bottom.equalTo(self.view);
             make.bottom.equalTo(self.view);
         }
     }];
    self.jznWebView.scrollView.bounces = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/examResult" type:@"html"];
    
    [self registJSFunction];
}
-(void) registJSFunction{
    [self.webViewBridge registerHandler:@"toAnswerSheet" handler:^(id data, WVJBResponseCallback responseCallback) {
        dispatch_async(dispatch_get_main_queue(), ^(){
            AnswerSheetViewController *vc = [[AnswerSheetViewController alloc]init];
            NSData *jsonData = [self.paper dataUsingEncoding:NSUTF8StringEncoding];
            NSError *err;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                options:NSJSONReadingMutableContainers
                                                                  error:&err];
            vc.paper = [dic[@"questionList"] mj_JSONString];
            [self.navigationController pushViewController:vc animated:YES];
        });
    }];
    
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    NSString *clear = [NSString stringWithFormat:@"getExamPaper(%@)",self.paper];
       [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
           
       }];
}
-(void) goBack{
      UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:0];
      [self.navigationController popToViewController:vc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
